package com.tongji.housediy.view.component3D
{
	import alternativa.engine3d.materials.TextureMaterial;
	
	import flash.display.BitmapData;
	import flash.display.GradientType;
	import flash.display.Shape;
	import flash.geom.Matrix;
	import flash.geom.Point;
	
	public class GradientMaterial extends TextureMaterial
	{
		private var matr:Matrix;
		private var spot:Shape;
		
		private var centerPoint:Point;
		private var cornerColor:Number;
		private var wholeColor:Number;
		private var fillRange:Number;
		
		public function GradientMaterial(centerPoint:Point, fillRange:Number, cornerColor:Number = 0xffffff, wholeColor:Number = 0xffffff, texture:BitmapData = null, repeat:Boolean=false, smooth:Boolean=true)
		{	
			if (texture == null)
				texture = new BitmapData(128, 128, false);
			
			this.centerPoint = centerPoint;
			this.fillRange = fillRange;
			this.cornerColor = cornerColor;
			this.wholeColor = wholeColor;
			
			super(texture, repeat, smooth);
			
			matr = new Matrix();
			matr.createGradientBox(fillRange, fillRange, 0, centerPoint.x - fillRange / 2, centerPoint.y - fillRange / 2);
			
			spot = new Shape();
			drawSpot();
		}
		
		public function resetMaterial(point:Point = null, fillRange:Number = -1, wholeColor:Number = -1, cornerColor:Number = -1):void
		{
			if (point != null)
				this.centerPoint = point;
			
			if (fillRange != -1)
				this.fillRange = fillRange;
			
			if (wholeColor != -1)
				this.wholeColor = wholeColor;
			
			if (cornerColor != -1)
				this.cornerColor = cornerColor;
			
			matr.createGradientBox(fillRange, fillRange, 0, centerPoint.x - fillRange / 2, centerPoint.y - fillRange / 2);
			
			drawSpot();
		}
		
		private function drawSpot():void
		{
			spot.graphics.clear();
			spot.graphics.beginGradientFill(GradientType.RADIAL, [wholeColor, 0x000000], [1, 1], [0, 255], matr);
			spot.graphics.drawRect(0, 0, 128, 128);
			if (cornerColor != 0xffffff)
			{
				spot.graphics.beginFill(cornerColor);
				spot.graphics.drawRect(0, 0, 128, 10);
			}
			texture.draw(spot);
		}
	}
}